<?php

namespace Users {

    class Controller_Users extends \Controller_Base {

        public $secure_actions = array('index' => NULL, 'settings' => NULL, 'logout' => NULL);

        public function before()
        {
            parent::before();
        }

        public function action_index()
        {
            $name = $this->param('name');

            if (!$name)
            {
                \Output::redirect('/');
            }

            $this->template->title = 'Welcome';
        }

        public function action_settings()
        {
            //$user = Model_User::find($this->user_id);

            $this->template->scripts[] = 'password_strength.js';
            $this->template->title = 'Settings';
        }

        public function action_login()
        {
            if ($this->logged_in)
            {
                \Output::redirect('/');
            }

            $val = \Validation::factory('users');
            $val->add_field('username', 'Your username', 'required|min_length[3]|max_length[20]');
            $val->add_field('password', 'Your password', 'required|min_length[3]|max_length[20]');

            if ($val->run())
            {
                $auth = \Auth::instance();

                if($auth->login($val->validated('username'), $val->validated('password')))
                {
                    \Session::set_flash('notice', 'FLASH: logged in');
                    \Output::redirect('users');
                }
                else
                {
                    $data['username']    = $val->validated('username');
                    $data['login_error'] = 'Wrong username/password combo. Try again';
                }
            }
            else
            {
                if($_POST)
                {
                    $data['username'] = $val->validated('username');
                    $data['error'] = 'Wrong username or password.';
                }
                else
                {
                    $data['error'] = null;
                }
            }

            $this->template->scripts[] = 'commons.js';
            $this->template->title = 'Login';
            $this->template->set($data);
        }

        public function action_logout()
        {
            \Auth::instance()->logout();

            \Session::set_flash('notice', 'FLASH: logged out');
            \Output::redirect('users');
        }

        public function action_signup()
        {
            if ($this->logged_in)
            {
                \Output::redirect('/');
            }

            $reCaptcha = \Recaptcha::instance();

            $validation = \Validation::factory('users2');
            $validation->add_callable('Morevalidation');
            $validation->set_message('captcha', 'The :label wasn\'t entered correctly. Go back and try it again.');

            $validation->add_field('username', 'Your username', 'required|min_length[3]|max_length[20]');
            $validation->add_field('password', 'Your password', 'required|min_length[3]|max_length[20]');
            $validation->add_field('email', 'Email', 'required|valid_email');
            $validation->add('captcha', 'reCaptcha')->add_rule('recaptcha', $reCaptcha);

            if ($validation->run())
            {
                if(\Auth::instance()->create_user($validation->validated('username'), $validation->validated('password'), $validation->validated('email'), '100'))
                {
                    \Session::set_flash('notice', 'FLASH: User created.');
                    \Output::redirect('users');
                }
                else
                {
                    throw new Exception('Smth went wrong while registering');
                }
            }
            else
            {
                if($_POST)
                {
                    $data['errors'] = array();
                    $data['username'] = $validation->validated('username');

                    $fields = array('username', 'password', 'email', 'captcha');
                    foreach ($fields as $field)
                    {
                        $message = $validation->errors($field)->get_message();
                        if (! empty($message))
                        {
                            $data['errors'][$field] = $message;
                        }
                    }
                }
                else
                {
                    $data['errors'] = null;
                }
            }

            $this->template->scripts[] = 'password_strength.js';
            $this->template->title = 'Sign Up';
            $this->template->set($data);
            $this->template->captcha = $reCaptcha->get_html();
        }

        public function action_activate()
        {
            $this->template->title = 'Activate Your Account';
        }

    } // End: Controller_Users

}